package Units;

import java.util.logging.Logger;

import main.MainApp;

import com.jme3.animation.AnimChannel;
import com.jme3.animation.AnimControl;
import com.jme3.animation.AnimEventListener;
import com.jme3.app.Application;
import com.jme3.input.KeyInput;
import com.jme3.input.controls.ActionListener;
import com.jme3.input.controls.KeyTrigger;
import com.jme3.light.DirectionalLight;
import com.jme3.math.Vector3f;
import com.jme3.scene.Node;
import com.jme3.scene.Spatial;

public class Creat {
	private static final Logger logger = Logger.getLogger(Application.class
			.getName());
	
	private static final String claiming = "Claiming";
	private static final String running = "Running";
	private static final String standing = "Standing";
	private static final String digging = "Digging";
	private static final String chilling = "Chilling";
	private static final String dragging = "Dragging";
	
	private AnimChannel channel;
	private AnimControl control;
	private UnitNode impNode;
	private String nextAnim = standing;
	 
	public Creat (Node modelsNode, int x, int y, Spatial imp) {
		impNode = new UnitNode("Imp", this);
		impNode.attachChild(imp);
		impNode.move(x, y, 0);
		
		modelsNode.attachChild(impNode);
		
		control = imp.getControl(AnimControl.class);
		control.addListener(animEventListener);
		channel = control.createChannel();
		channel.setAnim(standing);
	    MainApp.inMa.addMapping("run/stand", new KeyTrigger(KeyInput.KEY_SPACE));
	    MainApp.inMa.addMapping("chil", new KeyTrigger(KeyInput.KEY_R));
	    MainApp.inMa.addListener(actionListener, "run/stand", "chil");
	}
	
	private AnimEventListener animEventListener = new AnimEventListener() {
		public void onAnimCycleDone(AnimControl control, AnimChannel channel, String animName) {
			if (!animName.equals(nextAnim)) {
				channel.setAnim(nextAnim, 0.50f);
				channel.setSpeed(1f);
			}
		}
	 
		public void onAnimChange(AnimControl control, AnimChannel channel, String animName) {
			// unused
		}
	};
	
	public void setMode(String st) {
		if (claiming.equals(st)) {
			nextAnim = claiming;
		} else if (running.equals(st)) {
			nextAnim = running;
		} else if (standing.equals(st)) {
			nextAnim = standing;
		} else if (digging.equals(st)) {
			nextAnim = digging;
		} else if (chilling.equals(st)) {
			nextAnim = chilling;
		} else if (dragging.equals(st)) {
			nextAnim = dragging;
		}
	}

	private ActionListener actionListener = new ActionListener() {
		public void onAction(String name, boolean keyPressed, float tpf) {
			if (name.equals("run/stand") && !keyPressed) {
				if (!channel.getAnimationName().equals(running)) {
					nextAnim = running;
				} else {
					nextAnim = standing;
				}
			} else if (name.equals("chil") && !keyPressed) {
				if (!channel.getAnimationName().equals(chilling)) {
					nextAnim = chilling;
				} else {
					nextAnim = standing;
				}
			}
		}
	};
}